var fs = require('fs')
var path = require('path')
var axios = require('axios')

// 加载购彩号 
var data = fs.readFileSync(path.join(__dirname, 'double_color_balls.purchased.txt'), 'utf8').split('\n')

function run(order) {
    order = order ? order : 0
    // 中奖运算
    axios.get('http://f.apiplus.net/ssq.json')
        .then(function (response) {
            console.log('开奖日期：' + response.data.data[order].opentime)
            var facts = response.data.data[order].opencode.match(/\d\d/g)
            console.log('开奖号码：' + facts.join(' '))
            console.log('中奖情况：')

            data.forEach(function (dcb) {
                var balls = dcb.match(/\d\d/g)
                var rb = 0
                for (var i = 0; i < 6; i++) {
                    for (var j = 0; j < 6; j++) {
                        if (facts[i] === balls[j]) {
                            rb++
                        }
                    }
                }
                var bb = facts[6] === balls[6] ? 1 : 0
                var win = '未中奖 奖金：0'
                if (bb === 1 && rb < 3) {
                    win = '六等奖 奖金：5'
                } else if ((rb === 4 && bb === 0) || (rb === 3 && bb === 1)) {
                    win = '五等奖 奖金：10'
                } else if ((rb === 5 && bb === 0) || (rb === 4 && bb === 1)) {
                    win = '四等奖 奖金：200'
                } else if (rb === 5 && bb === 1) {
                    win = '三等奖 奖金：3000'
                } else if (rb === 6 && bb === 0) {
                    win = '二等奖 奖金：浮动'
                } else if (rb === 6 && bb === 1) {
                    win = '一等奖 奖金：浮动'
                }
                console.log('红球：' + rb, '蓝球：' + bb, win)
            })
        })
        .catch(function (error) {
            console.log(error.Error);
        })
}

exports.run = run